<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/

namespace Admin\Controller;
use Think\Controller;
class FuwuController extends IndexController {
	
	//列表动作-----------------------------------------------------------------------------------------------
	public function indexAction(){
		
		$model=new \Admin\Model\DataModel();
		
		$sqlwhere=array(' type=%d ',array('type'=>2));
		
		foreach($model->IndexGetData(
				//排序
				array(
					array('order'=>'id','name'=>'ID'),
					array('order'=>'subject','name'=>'名称'),
					array('order'=>'uid','name'=>'用户UID'),
					array('order'=>'username','name'=>'用户名'),
					array('order'=>'catid','name'=>'分类'),
					array('order'=>'money','name'=>'出售金额'),
					array('order'=>'process','name'=>'状态'),
					array('order'=>'top','name'=>'推荐'),
					array('order'=>'dateline','name'=>'发布时间'),
				),
				//查询
				array(	
					'subject'=>array('subject','search'),
					'id'=>array('id','int'),
					'uid'=>array('uid','int'),
					'process'=>array('process','int'),
					'cityid'=>array('cityid','city'),
					'catid'=>array('catid','catid'),
					'dateline1'=>array('dateline','time'),
					'dateline2'=>array('dateline','time'),
					
				),
				'id DESC'
				,
				20				
				,
				$sqlwhere
				
			) as $key=>$value){
			$this->assign($key,$value);
		}
		
		
			
		$category=array();
		if(!empty($_GET['catid'])){
			$catid=intval($_GET['catid']);
			$categoryarr=M('Category')->token(false)->where(" catid=$catid ")->find();
			if(!empty($categoryarr['catid'])){
				$category['category_name_1']=$categoryarr['name'];
				$upid=$categoryarr['upid'];
				$categoryarr=M('Category')->token(false)->where(" catid=$upid ")->find();
				if(!empty($categoryarr['catid'])){
					$category['category_name_2']=$categoryarr['name'];
					$upid=$categoryarr['upid'];
					$categoryarr=M('Category')->token(false)->where(" catid=$upid ")->find();
					if(!empty($categoryarr['catid'])){
						$category['category_name_3']=$categoryarr['name'];
					}
				}
			}
		}
		
		$this->assign('category',$category);
		

		$this->display();
	}

	//修改动作-----------------------------------------------------------------------------------------------
	public function editAction(){
		$id=I('get.id',0,'int');
		$model=new \Admin\Model\DataModel();
		$manage=$model->token(false)->where(" id=$id ")->find();
		if(!$manage['id']){
			$this->error('修改数据不存在!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/index'),3);
		}
			
		if(IsPost('Submit')){
			$datamodel=new \Data\Model\DataModel();

			$data=$datamodel->FormatData('fuwu');
			unset($data['uid']);
			unset($data['username']);
			if(C('otoo_open')){
				unset($data['provid']);
				unset($data['cityid']);
				unset($data['distid']);
				unset($data['longitude']);
				unset($data['latitude']);
				unset($data['provname']);
				unset($data['cityname']);
				unset($data['distname']);
			}
			
			$result=$model->EditData($data);
	
			if($result['id']){
				$upload=new \Upload\Model\FileModel();
				$upload->UseFile('sid',$id);
				$file = $upload->token(false)->where(" sid=$id AND thumbcut is not null ")->order('dateline ASC')->find();
				if(!empty($file['thumbcut'])){
					M('Data')->where("id=$id")->save(array('picture'=>$file['thumbcut']));
				}
			
				$this->ajaxReturn(array('title'=>'修改提示','message'=>'修改成功!','status'=>'ok','refresh'=>true));
			}else{
				$this->ajaxReturn(array('title'=>'验证失败','message'=>$model->getError(),'status'=>'error'));
			}
		}else{
			$this->assign('manage',$manage);
			$this->assign('id',$manage['id']);
			$this->display('Fuwu:add');
		}
		
	}
	
	//服务操作-----------------------------------------------------------------------------------------------
	public function operateAction(){
		

		//TOP------------------------------------------ 
		 $tasktop=0;
		 
		 if(!empty($_POST['ListTop'])){  

				$tasktop=1;
		 }
		 
		 if(!empty($_POST['ListTop']) || !empty($_POST['TopClose'])){   
		 
				$ids=GetCheckBox($_POST['checkbox']);
				
				if($ids){
					$idarr=explode(',',$ids);
				  
					foreach($idarr as $id){
						M('Data')->where(" id='$id' ")->save(array('top'=>$tasktop));
					}
				   
					$refresh='设置成功！';
				}else{
					$refresh='没有数据提交！';
				}

		 }elseif(!empty($_POST['CloseVerify'])){
			   
				$ids=GetCheckBox($_POST['checkbox']);
			  
				if($ids){
					
					M('Data')->token(false)->where(" id IN($ids) ")->save(array('process'=>0));
					
					$refresh.='<p>'.$ids.'号服务取消审核成功！</p>';
							 
				 }else{
					   
					   $refresh='<p>请选操作数据！</p>';
					 
				 }
		  
		   }elseif(!empty($_POST['Verify'])){
				
				$ids=GetCheckBox($_POST['checkbox']);
				
				if($ids){
					
					M('Data')->token(false)->where(" id IN($ids) ")->save(array('process'=>4));
					
					$refresh.='<p>'.$ids.'号服务审核成功！</p>';
					 
				 }else{
					   
					   $refresh='<p>请选操作数据！</p>';
					 
				 }	
					
		  }else{
				$_POST['Delete']=1;
				return $this->deleteAction();
		  
		  }

		$this->error($refresh,U(MODULE_NAME.'/'.CONTROLLER_NAME.'/index'),3);
	}
	
	//删除动作-----------------------------------------------------------------------------------------------
	public function deleteAction(){
		$model=new \Admin\Model\DataModel();
		$result=$model->DeleteData();
		$this->error($result['message'],U(MODULE_NAME.'/'.CONTROLLER_NAME.'/index'),3);
	}
	
}